<?php
session_start();

//if (!isset($_SESSION['role'])) {
 //   header('Location: http://localhost/Library_management_system/index.php');
 //   exit();
//}

$host = '127.0.0.1';  // 数据库主机
$dbname = 'library';  // 数据库名称
$username = 'root';  // 数据库用户名
$password = '123456';  // 数据库密码

// 创建 PDO 实例并设置错误模式
try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}

// 如果表单被提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 获取用户输入的值
    $reader_No = trim($_POST['reader_No']);
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
  

   

    // 插入数据到数据库
    $sql = "INSERT INTO users (u_No, reader_No, state,u_password) VALUES (:username, :reader_No, '正常', :password)";
    $stmt = $pdo->prepare($sql);

    try {
        // 执行插入操作
        $stmt->execute([
            ':username' => $username,
            ':password' => $password,
            ':reader_No' => $reader_No,
     
        ]);

        // 注册成功后，跳转到登录页面
        header("Location: http://localhost/Library_management_system/index.php");
        exit();
    } catch (PDOException $e) {
        $error_message = "注册失败： " . $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理系统 - 注册</title>
    <link rel="stylesheet" href="register_style.css"> <!-- 引入外部 CSS 文件 -->
</head>
<body>
    <!-- 页眉 -->
    <header>
        <h1>图书管理系统</h1>
    </header>

    <div class="register-container">
        <h2>读者注册</h2>

        <?php if (isset($error_message)): ?>
            <div class="error-message"><?php echo htmlspecialchars($error_message); ?></div>
        <?php endif; ?>

        <form action="register.php" method="POST">
            <div>
                <label for="username">账号：</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div>
                <label for="reader_No">读书证编号：</label>
                <input type="text" id="reader_No" name="reader_No" required>
            </div>
            <div>
                <label for="password">密码：</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div>
                <button type="submit">注册</button>
            </div>
        </form>

        <p>已有账户？<a href="http://localhost/Library_management_system/index.php">点击这里登录</a></p>
    </div>

    <!-- 页脚 -->
    <footer>
        <p>&copy; 2024 图书管理系统 | 版权所有 HMY,GSM</p>
    </footer>
</body>
</html>
